export const add = (foodinfo) => ({ "type": "ADD", foodinfo });
export const minus = (foodinfo) => ({ "type": "CUT", foodinfo });
export const clear = () => ({ "type": "CLEAR" });
export const initstoreinfo=(info)=>({"type":"INIT",info})


// export const welcomeUser = (params) => async (dispatch) => {
//     var  result  = await fetch("/foodbever/wechat/authorize?" + params).then(data => data.json());
//     dispatch({ "type": "welcomeUser", result });
// }
export const showFood = (params) => async (dispatch) => {
    var  result  = await fetch("/foodbever/buyer/product/list?" + params).then(res=>(res.json()));
    dispatch({ "type": "showFood", result});
}

export const orderInfo = (params) => async (dispatch) => {
    var  result  = await fetch("/foodbever/buyer/order/detail?" + params).then(res=>(res.json()));
    dispatch({ "type": "orderInfo", result });
}

export const createOrder = (params, postinfo) => async (dispatch) => {
    postinfo=postinfo.map((item)=>({
        "productId":item.id,
        "productQuantity":item.quantity
    }))
    
    let formData = new FormData();
        formData.append("tables",parseInt(JSON.parse(params.userOpenid).tableId));
        formData.append("userOpenid", JSON.parse(params.userOpenid).userOpenid,);
        formData.append("data",JSON.stringify(postinfo) );
    var  result  = await fetch("/foodbever/buyer/order/create", {
        method: 'POST',
         body:formData
    }).then(res=>(res.json()));
    dispatch({ "type": "createOrder", result })
}

